package com.BigDate.server.utils;

import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.util.Random;

/**
 * python操作的工具类
 * @author Suntingxing
 * @date 2022/1/1 1:38
 */
public class pyUtils {

    /**
     * 随机获得一个端口号
     * @return
     */
    public static String initPort(){
        Random random=new Random();
        //random.nextInt(max - min) + min + 1
        int randNum = random.nextInt(9000 - 8200) + 8200 + 1;
        return String.valueOf(randNum);
    }

    /**
     * 监测端口号是否可用(监听该端口是否被占用)
     * @param port
     * @return
     */
    public static boolean checkPort(String port){
        String a = ("cmd /k netstat -aon|findstr " + port);
        System.out.println("执行的指令:"+a);
        String res ="";
        int b=0;
        try {
            Process p = Runtime.getRuntime().exec(a);
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = null;
            while ((line = br.readLine())!=null){
                res += line;
                b++;
                if(b==1){
                    break;
                }
            }
            br.close();
            b=0;
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(res.isEmpty()){
            System.out.println("字符串为空 端口未被占用");
            return false;
        }
        String portStatus = pyUtils.getPortStatus(port);
        System.out.println("端口状况:"+portStatus);
        System.out.println("获取到的端口状态:"+portStatus.substring(portStatus.length()-1));
        if(portStatus.substring(portStatus.length()-1).equals("0")){
            System.out.println("端口未被占用");
            return false;
        }
        System.out.println("端口被占用");
        return true;
    }

    public static String getPortStatus(String port){
        String a = ("cmd /k netstat -aon|findstr " + port);
        System.out.println("执行的指令:"+a);
        String res ="";
        int b=0;
        try {
            Process p = Runtime.getRuntime().exec(a);
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line = null;
            while ((line = br.readLine())!=null){
                res += line;
                b++;
                if(b==1){
                    break;
                }
            }
            br.close();
            b=0;
            return res;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getPid(String port){
        String portStatus = pyUtils.getPortStatus(port);
        int end = portStatus.length();
        String substring = portStatus.substring(end - 9);
        String pid = substring.trim();
        return pid;
    }

    public static String killAndSave(String pid,String userWorkPath){
        String cmd = "taskkill /pid "+pid+"  -t  -f";
        System.out.println("杀死路径:");
        String cmd1 = "jupyter nbconvert --to markdown C:\\Users\\11920\\.jupyter\\"+userWorkPath +"\\1.ipynb";
        System.out.println("保存指令:"+cmd1);
        System.out.println("保存的工作目录:"+userWorkPath);
        String url="";
        try {
            Runtime.getRuntime().exec(cmd);
            Runtime.getRuntime().exec(cmd1);
            File saveFile = new File("C:\\Users\\11920\\.jupyter\\"+userWorkPath+"\\1.md");
            System.out.println("保存的文件:"+saveFile.getPath());
            while (true){
                if(saveFile.exists()){
                    FileInputStream fileInputStream = new FileInputStream(saveFile);
                    MultipartFile multipartFile = new MockMultipartFile(saveFile.getName(), saveFile.getName(),
                            ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
                    String[] filePath = FastDFSUtils.upload(multipartFile);
                    url = FastDFSUtils.getTrackerUrl() + filePath[0] + "/" +filePath[1];
                    break;
                }
            }
            return url;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return url;
    }

    public static void close(String pid){
        String cmd = "taskkill /pid "+pid+"  -t  -f";
        try {
            Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
